<?php
namespace Common\ORG\Qy;

/**
 * 订阅用户管理
 * 
*/

use \Common\ORG\Tools\Http;

class User extends Base{
        const URL_CREATE = 'https://qyapi.weixin.qq.com/cgi-bin/user/create';
        const URL_UPDATE = 'https://qyapi.weixin.qq.com/cgi-bin/user/update';
        const URL_DELETE = 'https://qyapi.weixin.qq.com/cgi-bin/user/delete';
        const URL_BATCH_DELETE = 'https://qyapi.weixin.qq.com/cgi-bin/user/batchdelete';
        const URL_GET = 'https://qyapi.weixin.qq.com/cgi-bin/user/get';
        const URL_LIST = 'https://qyapi.weixin.qq.com/cgi-bin/user/simplelist';
        const URL_DETAIL_LIST = 'https://qyapi.weixin.qq.com/cgi-bin/user/list';

        //订阅用户状态
        const STATUS_ALL = 0;//全部用户
        const STATUS_SUBSCRIB = 1; //已关注
        const STAtUS_BAN = 2; //禁用
        const STATUS_UNSUBSCRIB = 4; //未关注成

        //初始化函数
        public function __construct($token)
        {
            $this->_token = $token;
        }

        /**
         * 添加企业号成员
         * 
         * @param string  $userid 成员ID  
         * @param string  $name   姓名
         * @param array   $english_name 英文名
         * @param array   $department 部门节点
         * @param array   $order 部门内的排序值，默认为0。数量必须和department一致，数值越大排序越前面。有效的值范围是[0, 2^32)
         * @param string  $gender 性别。1表示男性，2表示女性
         * @param string  $position 职位
         * @param string  $mobile   电话号码
         * @param string  $email    邮箱
         * @param string  $telephone    座机。长度0-64个字节。
         * @param string  $isleader 上级字段，标识是否为上级。
         * @param string  $extattr  扩展参数，请参考微信接口
         * @param string  $avatar_mediaid 成员头像素材ID 
         * @param string  $enable 启用/禁用成员。1表示启用成员，0表示禁用成员
         * 
         * @return array('rcode' => 1/0 成功或者失败
         *               'msg'   => 提示信息
         *               'data'   => 返回数据 
         *              )
        */
        public function create($userid, $name, $department, $mobile, $position = null , $gender=null, $email = null, $enable = null,
             $english_name = null, $order = null, $telephone = null, $isleader = null, $extattr = null, $avatar_mediaid = null) 
        {
            $ret = $this->m_ret;
            $url = self::URL_CREATE.'?access_token='.urlencode($this->_token);
            $data = array('userid'=>$userid, 'name'=>$name ,'department'=>$department, 'mobile'=>$mobile);
            if ($english_name)  $data['english_name'] = $english_name;
            if ($order)  $data['order'] = $order;
            if ($position)  $data['position'] = $position;
            if ($gender)    $data['gender'] = $gender;
            if ($email)     $data['email'] = $email;
            if ($extattr)   $data['extattr'] = $extattr;
            if ($avatar_mediaid)    $data['avatar_mediaid'] = $avatar_mediaid;
            if ($telephone)   $data['telephone'] = $telephone;
            if ($isleader)   $data['isleader'] = $isleader;
            if ($enable)   $data['enable'] = $enable;
            $param = $this->_param($data);
            $res = Http::post($url, $param);
            $crackData = $this->crackRes( $res );
            $ret['rcode'] = $crackData['rcode'];
            $ret['msg'] = $crackData['rcode'] ? 'OK':$crackData['msg'];
            $ret['data'] = isset($crackData['data']) ? $crackData['data']:false;
            return $ret;
        }

        /**
         * 更新企业号成员
         * 
         * @param string  $userid 成员ID  
         * @param string  $name   姓名
         * @param array   $english_name 英文名
         * @param array   $order 部门内的排序值，默认为0。数量必须和department一致，数值越大排序越前面。有效的值范围是[0, 2^32)
         * @param array   $department 部门节点
         * @param string  $position 职位
         * @param string  $mobile   电话号码
         * @param string  $gender 性别。1表示男性，2表示女性
         * @param string  $email    邮箱
         * @param string  $telephone 座机
         * @param string  $isleader 上级字段，标识是否为上级
         * @param string  $extattr  扩展参数，请参考微信接口
         * @param string  $avatar_mediaid 成员头像素材ID 
         * @param string  $enable 启用/禁用成员。1表示启用成员，0表示禁用成员
         * 
         * @return array('rcode' => 1/0 成功或者失败
         *               'msg'   => 提示信息
         *               'data'   => 返回数据 
         *              )
         * 
        */
        public function update($userid, $name = null, $department = null, $mobile = null, $position = null,$gender = null, $email = null,
         $enable = null, $english_name = null, $order = null, $telephone = null, $isleader = null, $extattr = null, $avatar_mediaid = null)
        {
            $ret = $this->m_ret;
            $url = self::URL_UPDATE.'?access_token='.urldecode( $this->_token);
            $data = array('userid'=>$userid);
            if ($name)    $data['name'] = $name;
            if ($english_name)    $data['english_name'] = $english_name;
            if ($department)    $data['department'] = $department;
            if ($order)  $data['order'] = $order;
            if ($position)  $data['position'] = $position;
            if ($mobile)    $data['mobile'] = $mobile;
            if ($gender)    $data['gender'] = $gender;
            if ($email)     $data['email'] = $email;
            if ($telephone)   $data['telephone'] = $telephone;
            if ($isleader)   $data['isleader'] = $isleader;
            if ($extattr)   $data['extattr'] = $extattr;
            if ($avatar_mediaid)    $data['avatar_mediaid'] = $avatar_mediaid;
            if ($enable)    $data['enable'] = $enable;
            $param = $this->_param($data);
            $str = http::post($url, $param);
            $crackData = $this->crackRes( $str );
            $ret['rcode'] = $crackData['rcode'];
            $ret['msg'] = $crackData['rcode'] ? 'OK':$crackData['msg'];
            $ret['data'] = isset($crackData['data']) ? $crackData['data']:false;
            return $ret;
        }

        /**
         * 删除成员
         * 
        */
        public function delete($userid)
        {
            $ret = $this->m_ret;
            if (!$userid){
                $ret['msg'] = '参数错误';
            }else{
                $param = array();
                $param['access_token'] = $this->_token;
                $param['userid'] = $userid;
                $url = self::URL_DELETE.'?'.http_build_query( $param );
                $str = http::get( $url );
                $crackData = $this->crackRes( $str );
                $ret['rcode'] = $crackData['rcode'];
                $ret['msg'] = $crackData['rcode'] ? 'OK': $crackData['msg'];
                $ret['data'] = isset($crackData['data']) ? $crackData['data']:false;
            }
            return $ret;
        }

        /**
         * 批量删除成员
         * 
        */
        public function batchDelete($useridlist)
        {
            $ret = $this->m_ret;
            if (!$useridlist){
                $ret['msg'] = '参数错误';
            }else{
                $url = self::URL_BATCH_DELETE.'?access_token='.urldecode( $this->_token );
                $data = array('useridlist'=>$useridlist);
                $param = $this->_param($data);
                $str = http::post( $url, $param );
                $crackData = $this->crackRes( $str );
                $ret['rcode'] = $crackData['rcode'];
                $ret['msg'] = $crackData['rcode'] ? 'OK': $crackData['msg'];
                $ret['data'] = isset($crackData['data']) ? $crackData['data']:false;
            }
            return $ret;
        }

        /**
         * 获取成员
         * 
        */
        public function get($userid)
        {
            $ret = $this->m_ret;
            if (!$userid){
                $ret['msg'] = '参数错误';
            }else{
                $param = array();
                $param['access_token'] = $this->_token;
                $param['userid'] = $userid;
                $url = self::URL_GET.'?'.http_build_query( $param );
                $str = http::get( $url );
                $crackData = $this->crackRes( $str );
                $ret['rcode'] = $crackData['rcode'];
                if ($crackData['rcode']&&isset($crackData['data'])){
                    $ret['msg'] = 'OK';
                    $ret['data'] = isset($crackData['data']) ? $crackData['data']:false;
                }else
                    $ret['msg'] = $crackData['msg'];
                return $ret;
            }

        }

        /**
         * 获取部门成员
         * 
        */
        public function getList($department_id, $fetch_child = null)
        {
            $ret = $this->m_ret;
            $data = array();
            $data['access_token'] = urlencode($this->_token);
            $data['department_id'] = $department_id;
            if (null !== $fetch_child)
                $data['fetch_child'] = $fetch_child ? 1:0;
            $url = self::URL_LIST.'?'.http_build_query($data);
            $str = http::get($url);
            $crackData = $this->crackRes($str);
            $ret['rcode'] = $crackData['rcode'];
            if ($ret['rcode']){
                $ret['data'] = $crackData['data']['userlist'];
            }
            $ret['msg'] = $ret['rcode'] ? 'OK':$crackData['msg'];
             return $ret;
        }

        /**
         * 获取部门成员(详情)
         * 
        */
        public function getDetailList( $department_id, $fetch_child = null )
        {
            $ret = $this->m_ret;
            $data = array();
            $data['access_token'] = urlencode($this->_token);
            $data['department_id'] = $department_id;
            if (null !== $fetch_child)
                $data['fetch_child'] = $fetch_child ? 1:0;
            $url = self::URL_DETAIL_LIST.'?'.http_build_query($data);
            $str = http::get($url);
            $crackData = $this->crackRes($str);
            $ret['rcode'] = $crackData['rcode'];
            if ($ret['rcode']){
                $ret['data'] = $crackData['data']['userlist'];
            }
            $ret['msg'] = $ret['rcode'] ? 'OK':$crackData['msg'];
             return $ret;
        }
}

?>